package com.sx.utils;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author : AdinZ  1454269@qq.com
 * @Project: Common
 * @Description: TODO
 * @date Date : 2018年12月01日 13:58
 **/

public class DateUtils {
    private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);

    private static TimeZone timeZone = TimeZone.getTimeZone("GMT+0800");


    /***
     * 自定义时间,格式, 格式化
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date,String format){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(timeZone);
        return simpleDateFormat.format(date);
    }
    public static String format(long time,String format){
        return format(new Date(time),format);
    }
    public static String format(String format){
        return format(new Date(),format);
    }

    public static Date parse(String date,String format) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(timeZone);
        return simpleDateFormat.parse(date);
    }


    /***
     * 当前时间格式化
     * @return
     */
    public static String format(){
       return  format(new Date());
    }

    /**
     * 时间格式化 2018-11-11 12:12:12
     * @param date
     * @return
     */
    public static String format(Date date){
        format.setTimeZone(timeZone);
        return  format.format(date);
    }
    public static String format(long time){
        return format(new Date(time));
    }

    /***
     * 获取年月 2018-11
     * @param date 时间
     * @return 2018-11
     */
    public static String formatYearMonth(Date date){
        return format(date,"yyyy-MM-dd");
    }

    /**
     * 获取当前时间的年月
     * @return
     */
    public static String formatYearMonth(){
        return formatYearMonth(new Date());
    }


    public static Date todayStart(int day){
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTimeZone(timeZone);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        currentDate.add(Calendar.DATE, day);
        return currentDate.getTime();
    }

    public static Date todayStart(Date date){
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.setTimeZone(timeZone);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        return currentDate.getTime();
    }
    public static Date todayEnd(Date date){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static Date todayEnd(int day){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.add(Calendar.DATE, day);
        Date date=calendar.getTime();
        return date;
    }

    public static Date getDate(){
        Calendar cal = Calendar.getInstance(timeZone);
        return cal.getTime();
    }
    public static long getTime(){
        return getDate().getTime();
    }

    public static Date getMonthStart(int month){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY)+month, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
    public static Date getMonthEnd(int month) {
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY)+month, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static Date getYearStart(int year){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR)+year, cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
    public static Date getYearEnd(int year){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR)+year, cal.getActualMaximum(Calendar.MONDAY), cal.getActualMaximum(Calendar.DAY_OF_MONTH), 0, 0, 0);
//        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static Date toDate(String date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(timeZone);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date toDate(int day){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.add(Calendar.DATE, day);
        Date date=calendar.getTime();
        return date;
    }

    public static Date toDate(String date){
        return toDate(date,"yyyy-MM-dd HH:mm:ss");
    }


    public static long addDay(int day) {
       return DateUtils.day(day).getTime();
    }
    public static Date day(int day){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH)+day, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
        return cal.getTime();
    }

    /**
     * 当前时间加x分钟
     * @param minute 分钟
     * @return
     */
    public static Date addMinute(int minute){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)+minute, cal.get(Calendar.SECOND));
        return cal.getTime();
    }

    /**
     * 当前时间加x小时
     * @param hour
     * @return
     */
    public static Date addHour(int hour){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY)+hour, cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
        return cal.getTime();
    }

    public static int getWeekOfDate(){
        return getWeekOfDate(new Date());
    }

    public static int getWeekOfDate(Date date){
        Calendar cal=new Calendar.Builder().setInstant(date).setTimeZone(timeZone).build();
        return cal.get(Calendar.DAY_OF_WEEK)-1;
    }

    public static boolean isWorkTime(Date date){
        int week=getWeekOfDate(date);
        Calendar cal=new Calendar.Builder().setInstant(date).build();
        cal.setTimeZone(timeZone);
        int hour= cal.get(Calendar.HOUR_OF_DAY);
        if(week==0||week==6){
            return false;
        }else {
            if(hour<9||hour>=17) return false;
            else return true;
        }
    }

    public static long getNextWeekTuesDay(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.TUESDAY);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - day+7);
        return calendar.getTimeInMillis();
    }


    public static long now(){
        return new Date().getTime();
    }




    public static long getToDayHour(int hour) {
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }
}
